<template>
  <div class="app-container">
    <el-card shadow="never" v-loading="loading">
      <el-row :gutter="5">
        <el-col :span="24" class="main-card-view">
          <el-row>
            <el-card shadow="never" class="top-search-view-card">
              <el-row>
                <el-form :inline="true" :model="searchForm" class="demo-form-inline search-view">
                  <el-form-item label="关键字">
                    <el-input v-model="searchForm.keyWord" placeholder="请输入关键字"></el-input>
                  </el-form-item>
                  <el-form-item label="分类">
                    <el-select v-model="searchForm.classify" placeholder="请选择">
                      <el-option v-for="item in ['问卷', '竞答']" :key="item" :label="item" :value="item">
                      </el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item>
                    <el-button type="primary" @click="handleSearch" class="search-button">查询</el-button>
                    <!--                                        <el-button type="success" @click="handleReset" class="reset-button">重置</el-button>-->
                    <el-button type="primary" @click="showCreateDialog" class="new-button">添加</el-button>
                  </el-form-item>
                </el-form>
              </el-row>
            </el-card>
          </el-row>
          <el-row>
            <el-card shadow="never">
              <el-table :data="tableData" :stripe="true" style="width: 100%">
                <el-table-column prop="classify" align="center" label="分类" />
                <el-table-column prop="activityName" align="center" label="活动名称" />
                <el-table-column prop="totalPoints" align="center" label="总分数" />
                <el-table-column prop="passingMark" align="center" label="及格分数" />
                <el-table-column prop="createTime" align="center" label="创建时间">
                  <template slot-scope="{row}">
                    {{ moment(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}
                  </template>
                </el-table-column>
                <el-table-column prop="answerStartTime" align="center" label="活动开始时间" />
                <el-table-column prop="answerEndTime" align="center" label="活动结束时间" />
                <el-table-column prop="questionNums" align="center" label="题目数">
                  <template slot-scope="{row}">
                    <el-button @click="openRelevance(row)" type="text">{{ row.questionNums }}</el-button>
                  </template>
                </el-table-column>
                <el-table-column align="center" label="操作" width="200">
                  <template slot-scope="scope">
                    <el-button type="text" class="table-edit-btn"
                      @click="showEditDialog(scope.$index, scope.row)"></el-button><!--编辑-->
                    <el-button type="text" class="table-delete-btn"
                      @click="handleDelete(scope.$index, scope.row)"></el-button><!--删除-->
                  </template>
                </el-table-column>
              </el-table>
              <XPagination ref="pageInfo" :total="total" :get-data="getList" />
            </el-card>
          </el-row>
        </el-col>
      </el-row>
    </el-card>
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible">
      <el-form label-width="120px" ref="form" :model="form" :rules="formRules">
        <el-form-item label="分类" prop="classify">
          <el-select v-model="form.classify" placeholder="请选择">
            <el-option v-for="item in ['问卷', '竞答']" :key="item" :label="item" :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动名称" prop="activityName">
          <el-input v-model="form.activityName" />
        </el-form-item>
        <el-form-item label="图片" prop="image">
          <x-upload v-model="form.image" />
        </el-form-item>
        <el-form-item label="位置">
          <el-checkbox v-model="positionOne">首页</el-checkbox>
          <el-checkbox v-model="positionsTwo">栏目首页</el-checkbox>
        </el-form-item>
        <el-form-item label="是否栏目推荐" prop="columnRecommendation">
          <el-radio-group v-model="form.columnRecommendation">
            <el-radio label="否"></el-radio>
            <el-radio label="是"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否开启评论" prop="enableComments">
          <el-radio-group v-model="form.enableComments">
            <el-radio label="否"></el-radio>
            <el-radio label="是"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="关键字" prop="keyWord">
          <el-input v-model="form.keyWord" />
        </el-form-item>
        <el-form-item label="通过最低分" prop="keyWord">
          <el-input v-model="form.passingMark" />
        </el-form-item>
        <el-form-item label="答题时间" prop="answerStartTime">
          <el-date-picker v-model="timer" type="daterange" range-separator="至" start-placeholder="开始日期"
            end-placeholder="结束日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template slot="footer">
        <el-button type="primary" @click="handleSave">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import XPagination from "../../components/xpagination/XPagination";
import XUpload from "../../components/xupload/XUpload"
import relevance from "./relevance.vue";
import * as ActivityManagement from "../../api/ActivityManagement"
import * as formUtils from "../../utils/formUtils";
export default {
  name: "list",
  components: { XPagination, XUpload, relevance },
  data () {
    return {
      searchForm: {

      },
      tableData: [],
      loading: false,
      dialogVisible: false,
      treeDialog: false,
      relevanceDialog: false,
      total: 0,
      dialogTitle: "添加",
      form: {
      },
      timer: [],
      editStatus: "add",
      formRules: {
        roleName: [
          { required: true, message: '不能为空', trigger: 'blur' },
        ],
        description: [
          { required: true, message: '不能为空', trigger: 'blur' },
        ],
      },
      checkedArr: [],
      defaultProps: {
        children: 'children',
        label: 'columnName'
      },
      roleId: '',
      positionOne: false,
      positionsTwo: false,
    }
  },
  methods: {
    getList (pageInfo) {
      const param = {
        ...this.searchForm,
        ...pageInfo
      }
      ActivityManagement.queryByPage(param).then(response => {
        console.log(response)
        this.tableData = response.data.data.rows
        this.total = response.data.data.total
        this.loading = false
      }).catch(error => {
        console.log(error)
      })
    },
    handleDelete (index, data) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const param = {
          uid: data.uid,
        }
        this.loading = true
        ActivityManagement.deleteById(param).then(result => {
          this.loading = false
          this.$message.success(result.data.msg)
          this.handleSearch()
        }).catch(error => {
          this.loading = false
          console.log(error)
        })
      }).catch(() => {
        console.log('取消删除')
      });
    },
    handleSearch () {
      this.$refs.pageInfo.loadData()
    },
    showCreateDialog () {
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.dialogTitle = "添加"
        this.editStatus = "add"
        this.$refs.form.resetFields()
        this.form = {}
        this.positionOne = false
        this.positionsTwo = false
        this.timer = []
      })
    },
    handleSave () {
      if (this.positionOne) {
        this.form.positionOne = '首页'
      } else {
        this.form.positionOne = '否'
      }
      if (this.positionsTwo) {
        this.form.positionsTwo = '栏目首页'
      } else {
        this.form.positionsTwo = '否'
      }
      [this.form.answerStartTime, this.form.answerEndTime] = this.timer
      if (this.editStatus === "add") {
        this.handleAdd()
      } else {
        this.handleEdit()
      }
    },
    handleAdd () {
      this.$refs["form"].validate((valid) => {
        if (!valid) {
          return false
        }
        const param = Object.assign({}, this.form)
        formUtils.filterFormFields(param)
        ActivityManagement.insert(param).then(result => {
          this.$message.success(result.data.msg)
          this.dialogVisible = false
          this.handleSearch()
        }).catch(error => {
          this.$message.error(error)
          console.log(error)
        })

      })
    },
    handleEdit () {
      this.$refs["form"].validate((valid) => {
        if (!valid) {
          return false
        }
        const param = Object.assign({}, this.form)
        formUtils.filterFormFields(param)
        ActivityManagement.update(param).then(result => {
          this.$message.success(result.data.msg)
          this.dialogVisible = false
          this.handleSearch()
        }).catch(error => {
          this.$message.error(error)
          console.log(error)
        })
      })
    },
    showEditDialog (index, data) {
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.form.resetFields()
        this.form = Object.assign({}, data)
        if (this.form.positionOne == '首页') {
          this.positionOne = true
        } else {
          this.positionOne = false
        }
        if (this.form.positionsTwo == '栏目首页') {
          this.positionsTwo = true
        } else {
          this.positionsTwo = false
        }
        this.timer = [this.form.answerStartTime, this.form.answerEndTime]
        this.dialogTitle = "编辑"
        this.editStatus = "edit"
      })
    },
    openRelevance (row) {
      this.$router.push({
        path: 'topic',
        query: {
          uid: row.uid,
          classify: row.classify
        }
      })
    }
  },
  mounted () {
    this.handleSearch()
    this.getTree()
  }
}
</script>

<style scoped lang="scss"></style>
